﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;

namespace WXYW
{
    public partial class SpecialSetting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserService userService = new UserService();
                EngineerDDL.DataSource = userService.GetEngineers();
                EngineerDDL.DataTextField = "UserName";
                EngineerDDL.DataValueField = "UserID";
                EngineerDDL.DataBind();
            }
        }

        protected void DELRMABtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(DELRMANOTxtBox.Text))
                {
                    SpecialSettingService service = new SpecialSettingService();
                    int retCode = service.DeleteRMA(DELRMANOTxtBox.Text.Trim());
                    if (retCode > 0)
                    {
                        DELRMAMsgLbl.Text = string.Format("{0}已被成功删除！", DELRMANOTxtBox.Text);
                    }
                    else
                    {
                        DELRMAMsgLbl.Text = string.Format("{0}不存在，请确认单号填写是否正确！", DELRMANOTxtBox.Text);
                    }
                }
                else
                {
                    DELRMAMsgLbl.Text = string.Format("单号不能为空，请填写单号！");
                }
            }
            catch (Exception ex)
            {
                DELRMAMsgLbl.Text = string.Format("系统错误：{0}", ex.Message);
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("SpecialSetting -> DELRMABtn_Click RMANO:{0}", DELRMANOTxtBox.Text));
            }
        }

        protected void ChangeStatusBtn_Click(object sender, EventArgs e)
        {
            try
            {
                SpecialSettingService service = new SpecialSettingService();
                if (!string.IsNullOrEmpty(ChangeStatusNOTxtBox.Text))
                {
                    int retCode = service.ChangeStatusOfApplication(ChangeStatusNOTxtBox.Text, StatusDDL.SelectedValue);
                    if (retCode > 0)
                    {
                        MsgLbl.Text = string.Format("{0}的状态已被成功修改！", ChangeStatusNOTxtBox.Text);
                    }
                    else if (retCode == 0)
                    {
                        MsgLbl.Text = string.Format("{0}不存在，请确认单号是否填写正确！", ChangeStatusNOTxtBox.Text);
                    }
                }
                else
                {
                    MsgLbl.Text = string.Format("单号不能为空，请填写单号！");
                }
            }
            catch (Exception ex)
            {
                DELRMAMsgLbl.Text = string.Format("系统错误：{0}", ex.Message);
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("SpecialSetting -> DELRMABtn_Click RMANO:{0}", DELRMANOTxtBox.Text));
            }
        }

        protected void cpsButton_Click(object sender, EventArgs e)
        {
            try
            {
                SpecialSettingService service = new SpecialSettingService();
                if (!string.IsNullOrEmpty(cpsFormCodeTextBox.Text) && !string.IsNullOrEmpty(cpsProductSNTextBox.Text))
                {
                    int retCode = service.ChangeStatusOfApplicationProduct(cpsFormCodeTextBox.Text, cpsProductSNTextBox.Text, StatusDDL.SelectedValue);
                    if (retCode > 0)
                    {
                        MsgLbl.Text = string.Format("{0}的状态已被成功修改！", ChangeStatusNOTxtBox.Text);
                    }
                    else if (retCode == 0)
                    {
                        MsgLbl.Text = string.Format("{0}不存在，请确认单号是否填写正确！", ChangeStatusNOTxtBox.Text);
                    }
                }
                else
                {
                    MsgLbl.Text = string.Format("单号或序列号不能为空！");
                }
            }
            catch (Exception ex)
            {
                DELRMAMsgLbl.Text = string.Format("系统错误：{0}", ex.Message);
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("SpecialSetting -> DELRMABtn_Click RMANO:{0}", DELRMANOTxtBox.Text));
            }
        }

        protected void ChangeSNBtn_Click(object sender, EventArgs e)
        {
            try
            {
                SpecialSettingService service = new SpecialSettingService();
                if (!string.IsNullOrEmpty(ChangeSNNOTxtBox.Text))
                {
                    int retCode = service.ChangeSN(ChangeSNNOTxtBox.Text, OriginalSNTxtBox.Text, NewSNTxtBox.Text);
                    if (retCode > 0)
                    {
                        MsgLbl.Text = string.Format("{0}中序号为{1}的待修产品已被改为{2}！", ChangeSNNOTxtBox.Text, OriginalSNTxtBox.Text, NewSNTxtBox.Text);
                    }
                    else if (retCode == -1)
                    {
                        MsgLbl.Text = string.Format("单号为{0}的申请单不存在，请检查是否填写正确，注意去掉空格！", ChangeSNNOTxtBox.Text);
                    }
                    else if (retCode == -2)
                    {
                        MsgLbl.Text = string.Format("单号为{0}的申请单中不存在序列号为{1}的待修品，请检查是否填写正确，注意去掉空格！", ChangeSNNOTxtBox.Text, OriginalSNTxtBox.Text);
                    }
                }
                else
                {
                    MsgLbl.Text = string.Format("单号不能为空，请填写单号！");
                }
            }
            catch (Exception ex)
            {
                MsgLbl.Text = string.Format("系统错误：{0}", ex.Message);
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("SpecialSetting -> ChangeSNBtn_Click RMANO:{0}", ChangeSNNOTxtBox.Text));
            }
        }

        protected void ChangeBJXXStatusBtn_Click(object sender, EventArgs e)
        {
            try
            {
                SpecialSettingService service = new SpecialSettingService();
                if (!string.IsNullOrEmpty(ChangeBJXXStatusNOTxtBox.Text))
                {
                    if (ChangeBJXXStatusNOTxtBox.Text.StartsWith("B"))
                    {
                        int retCode = service.ChangeBJXXStatus(ChangeBJXXStatusNOTxtBox.Text, BJXXStatusDDL.SelectedValue);
                        if (retCode > 0)
                        {
                            MsgLbl.Text = string.Format("{0}的状态已被成功修改为{1}", ChangeBJXXStatusNOTxtBox.Text, BJXXStatusDDL.SelectedValue);
                        }
                        else
                        {
                            MsgLbl.Text = string.Format("不存在单号为{0}的备件先行申请单，请确认单号填写正确！", ChangeBJXXStatusNOTxtBox.Text);
                        }
                    }
                    else
                    {
                        MsgLbl.Text = string.Format("单号{0}不是一个合法的备件先行单号, 请确认填写是否正确！", ChangeBJXXStatusNOTxtBox.Text);
                    }
                }
                else
                {
                    MsgLbl.Text = string.Format("单号不能为空，请填写单号！");
                }
            }
            catch (Exception ex)
            {
                MsgLbl.Text = string.Format("系统错误：{0}", ex.Message);
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("SpecialSetting -> ChangeBJXXStatusBtn_Click BJXXNO:{0}", ChangeBJXXStatusNOTxtBox.Text));
            }
        }

        protected void QueryStatusBtn_Click(object sender, EventArgs e)
        {
            try
            {
                SpecialSettingService service = new SpecialSettingService();
                string status = service.GetStatusOfApplication(QueryStatusRMANOTxtBox.Text);
                CurrentStatusLbl.Text = status;
            }
            catch (Exception ex)
            {
                MsgLbl.Text = string.Format("系统错误：{0}", ex.Message);
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("SpecialSetting -> QueryStatusBtn_Click RMANO:{0}", QueryStatusRMANOTxtBox.Text));
            }
        }

        protected void ExchangeRMADOANOButton_Click(object sender, EventArgs e)
        {
            try
            {
                SpecialSettingService service = new SpecialSettingService();
                string newRMANO = string.Empty;
                int retCode = service.ExchangeRMADOA(ExchangeRMADOANOTextBox.Text, ref newRMANO);
                NewRMADOANOTxtBox.Text = newRMANO;
                if (retCode > 0)
                {
                    MsgLbl.Text = string.Format("{0}已被成功转换为{1}", ExchangeRMADOANOTextBox.Text, newRMANO);
                }
                else if (retCode == 0)
                {
                    MsgLbl.Text = string.Format("{0}不存在，请确认单号是否正确！", ExchangeRMADOANOTextBox.Text);
                }
                // -1
                else
                {

                }
            }
            catch (Exception ex)
            {
                MsgLbl.Text = string.Format("系统错误：{0}", ex.Message);
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("SpecialSetting -> QueryStatusBtn_Click RMANO:{0}", QueryStatusRMANOTxtBox.Text));
            }
        }

        protected void ChangeEngineerBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (!EngineerDDL.SelectedValue.Equals("0"))
                {
                    SpecialSettingService service = new SpecialSettingService();
                    int retCocde = service.ChangeEngineer(ChangeEngineerNOTxtBox.Text, ChangeEngineerSNTxtBox.Text, Convert.ToInt32(EngineerDDL.SelectedValue));
                    if (retCocde > 0)
                    {
                        MsgLbl.Text = string.Format("{0}已成功派单给{1}", ChangeEngineerNOTxtBox.Text, EngineerDDL.SelectedItem.Text);
                    }
                    else if (retCocde == -2)
                    {
                        MsgLbl.Text = string.Format("{0}里没有找序列号为{1}的待修产品，请确认序列号填写正确。", ChangeEngineerNOTxtBox.Text, ChangeEngineerSNTxtBox.Text);
                    }
                    else
                    {
                        MsgLbl.Text = string.Format("{0}不存在，请确认单号填写正确。", ChangeEngineerNOTxtBox.Text);
                    }
                }
                else
                {
                    MsgLbl.Text = "请选择你要转派的工程师！";
                }
            }
            catch (Exception ex)
            {
                MsgLbl.Text = string.Format("系统错误：{0}", ex.Message);
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("SpecialSetting -> QueryStatusBtn_Click RMANO:{0}", QueryStatusRMANOTxtBox.Text));
            }
        }

        protected void ChangeBJApplyStatusBtn_Click(object sender, EventArgs e)
        {
            try
            {
                SpecialSettingService service = new SpecialSettingService();
                if (!string.IsNullOrEmpty(ChgBJApplyCodeTxtBox.Text)) // 申请单流水号
                {
                    int retCode = service.ChangeBJApplyStatus(ChgBJApplyCodeTxtBox.Text, ChgBJApplyDropDownList.SelectedValue);
                    if (retCode > 0 && 9 != retCode)
                    {
                        MsgLbl.Text = string.Format("{0}的状态已被成功修改为{1}", ChgBJApplyCodeTxtBox.Text, ChgBJApplyDropDownList.SelectedValue);
                    }
                    else if (retCode == 9) 
                    {
                        MsgLbl.Text = string.Format("流水号为{0}的备件申请单的目前状态不能修改为{2}！", ChgBJApplyCodeTxtBox.Text, ChgBJApplyDropDownList.SelectedValue);
                    }
                    else
                    {
                        MsgLbl.Text = string.Format("不存在流水号为{0}的备件申请单，请确认流水号填写正确！", ChgBJApplyCodeTxtBox.Text);
                    }
                }
                else
                {
                    MsgLbl.Text = string.Format("流水号不能为空，请填写流水号！");
                }
            }
            catch (Exception ex)
            {
                MsgLbl.Text = string.Format("系统错误：{0}", ex.Message);
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("SpecialSetting -> ChangeBJApplyStatusBtn_Click BJApplyNO:{0}", ChgBJApplyCodeTxtBox.Text));
            }
        }
    }
}
